package com.banksoft.cps.controller.admin;

import com.banksoft.cps.bean.warpper.BaseStatisticsGroup;
import com.banksoft.cps.bean.StatisticsV2;
import com.banksoft.cps.bean.common.ServiceException;
import com.banksoft.cps.bean.query.StatisticsCommonQuery;
import com.banksoft.cps.bean.query.StatisticsQuery;
import com.banksoft.cps.bean.warpper.BaseStatistics;
import com.banksoft.cps.bean.warpper.StatisticsWarpper;
import com.banksoft.cps.controller.ControllerConstants;
import com.banksoft.cps.enums.TimeUnit;
import com.banksoft.cps.service.StatisticsService;
import com.banksoft.cps.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by rongzhao.zhu on 2018/1/16.
 */
@Controller
@RequestMapping("/admin/statistics/")
public class AdminStatisticsController extends ControllerConstants {

    @Autowired
    StatisticsService statisticsService;

    @RequestMapping(value = "find",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String find(StatisticsQuery query){
        StatisticsWarpper warpper = null;
        try{
             warpper = statisticsService.query(query);
        }catch (ServiceException e){
            return  toExceptionString(e);
        }
        return toJSONString(createDataTip(warpper));
    }


    @RequestMapping(value = "findV2",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String findV2(StatisticsQuery query){
        List<StatisticsV2> list = null;
        try{
            list = statisticsService.queryV2(query);
        }catch (ServiceException e){
            return  toExceptionString(e);
        }
        return toJSONString(createDataTip(list));
    }


    @RequestMapping(value = "findV3",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String findV3(StatisticsQuery query){
        List<StatisticsV2> list = null;
        try{
            list = statisticsService.queryV3(query);
        }catch (ServiceException e){
            return  toExceptionString(e);
        }
        return toJSONString(createDataTip(list));
    }

    @RequestMapping(value = "statistics",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String statistics(StatisticsCommonQuery query){
        List<BaseStatistics> list = null;
        try{
            list = statisticsService.statistics(query);
        }catch (ServiceException e){
            return  toExceptionString(e);
        }
        return toJSONString(createDataTip(list));
    }


    @RequestMapping(value = "statisticsSupplierOrders", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String statisticsSupplierOrders(StatisticsCommonQuery query){

        BaseStatisticsGroup baseStatisticsGroup = null;
        try {
            long now = System.currentTimeMillis();
            long begin = DateUtil.getbeforeMonthTime(now,-12);
            begin = DateUtil.getMonthFirstDay(begin);
            query.setTimeBegin(DateUtil.getDateStringByFormat(begin,"yyyy-MM-dd"));
            query.setTimeEnd(DateUtil.getDateStringByFormat(now,"yyyy-MM-dd"));
            query.setGroupBy(TimeUnit.month.name());
            baseStatisticsGroup = statisticsService.statisticsSupplierOrders(query);
        } catch (Exception e) {
            e.printStackTrace();
            return  toExceptionString(e);
        }
        return toJSONString(createDataTip(baseStatisticsGroup));
    }
    @RequestMapping(value = "statisticsSaleOrders", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String statisticsSaleOrders(StatisticsCommonQuery query){
        BaseStatisticsGroup baseStatisticsGroup = null;
        try {
            long now = System.currentTimeMillis();
            long begin = DateUtil.getbeforeMonthTime(now,-12);
            begin = DateUtil.getMonthFirstDay(begin);
            query.setTimeBegin(DateUtil.getDateStringByFormat(begin,"yyyy-MM-dd"));
            query.setTimeEnd(DateUtil.getDateStringByFormat(now,"yyyy-MM-dd"));
            query.setGroupBy(TimeUnit.month.name());
            baseStatisticsGroup = statisticsService.statisticsSaleOrders(query);
        } catch (Exception e) {
            e.printStackTrace();
            return  toExceptionString(e);
        }
        return toJSONString(createDataTip(baseStatisticsGroup));
    }

}
